﻿using FluentNHibernate;
using MadMediaManager.Core.DependencyInjectionSupport;
using MadMediaManager.Core.Interfaces.DataAccess;
using MadMediaManager.Core.Interfaces.DependencyInjectionSupport;
using MadMediaManager.Core.Interfaces.Services;
using MadMediaManager.DataAccess.Database;
using MadMediaManager.ViewModels.Home;
using MadMediaManager.ViewModels.Shell;
using Moq;
using NHibernate;
using NUnit.Framework;
using Ocean.OceanFramework.ViewModelUIServices;
using SharpTestsEx;

namespace MadMediaManager.Infrastructure.Tests
{
    [TestFixture]
    public class ContainerConfiguratorTest
    {
        [TestFixtureSetUp]
        public void TestFixtureSetUp()
        {
            new SqlCeDatabaseCreator("DataSource=TestDb.sdf").Delete().Create();
        }

        [TestFixtureTearDown]
        public void TestFixtureTearDown()
        {
            new SqlCeDatabaseCreator("DataSource=TestDb.sdf").Delete();
        }

        [SetUp]
        public void SetUp()
        {
            var appSettings = new Mock<IApplicationSettings>();
            appSettings.Setup(a => a.ConnectionString)
                .Returns("DataSource=TestDb.sdf");
            AppServiceContainer.Init(new ContainerConfigurator(appSettings.Object));
        }

        [TearDown]
        public void TearDown()
        {
            AppServiceContainer.RemoveConfiguration();
        }

        [Test]
        public void Get_ISessionSource_ReturnsSingletonSessionSource()
        {
            var sessionSource = AppServiceContainer.Get<ISessionSource>();
            sessionSource.Should().Not.Be.Null();
            sessionSource.Should().Be.SameInstanceAs(AppServiceContainer.Get<ISessionSource>());
        }

        [Test]
        public void Get_IDbContext_ReturnsTransientInstance()
        {
            var dbContext = AppServiceContainer.Get<IDbContext>();
            dbContext.Should().Not.Be.Null();
            dbContext.FieldValue<ISession>("_session").Should().Not.Be.Null();
            dbContext.Should().Not.Be.SameInstanceAs(AppServiceContainer.Get<IDbContext>());
        }

        [Test]
        public void Get_IEventPublisher_ReturnsSingletonInstance()
        {
            var publisher = AppServiceContainer.Get<IEventPublisher>();
            publisher.Should().Not.Be.Null();
            publisher.Should().Be.SameInstanceAs(AppServiceContainer.Get<IEventPublisher>());
        }

        [Test]
        public void Get_MainWindowViewModel_ReturnsTransientInstance()
        {
            var viewModel = AppServiceContainer.Get<MainWindowViewModel>();
            viewModel.Should().Not.Be.Null();
            viewModel.FieldValue<IViewModelUIService>("_uiService").Should().Not.Be.Null();
            viewModel.Should().Not.Be.SameInstanceAs(AppServiceContainer.Get<MainWindowViewModel>());
        }

        [Test]
        public void Get_HomeViewModel_ReturnsTransientInstance()
        {
            var viewModel = AppServiceContainer.Get<HomeViewModel>();
            viewModel.Should().Not.Be.Null();
            viewModel.FieldValue<IDbContext>("_dbContext").Should().Not.Be.Null();
            viewModel.FieldValue<IEventPublisher>("_eventPublisher").Should().Not.Be.Null();
            viewModel.Should().Not.Be.SameInstanceAs(AppServiceContainer.Get<HomeViewModel>());
        }

        [Test]
        public void Get_IViewModelUiService_ReturnsSingletonInstance()
        {
            var uiService = AppServiceContainer.Get<IViewModelUIService>();
            uiService.Should().Not.Be.Null();
            uiService.Should().Be.SameInstanceAs(AppServiceContainer.Get<IViewModelUIService>());
        }
    }
}
